package main

import (
	"github.com/streadway/amqp"
	"log"
	"fmt"
)

func isError2(err error, msg string) {
	if err != nil{
		log.Fatal(fmt.Sprintf("%s: %s", msg, err))
		panic(fmt.Sprintf("%s:%s", msg, err))
	}
}

func main(){
	conn, err := amqp.Dial("amqp://localhost")
	isError2(err, "failed to connect to rabbitmq")

	ch, err := conn.Channel()
	isError2(err, "failed to create channel")

	err = ch.ExchangeDeclare(
			"logs",
			"fanout",
			true,
			false,
			false,
			false,
			nil,
		)
	isError2(err, "failed to create rabbitmq")

	q, err := ch.QueueDeclare("", false,false,true,false, nil)
	isError2(err, fmt.Sprintf("failed to declare queue"))

	err = ch.QueueBind(q.Name, "", "logs", false, nil)
	isError2(err, fmt.Sprintf("failed to bind queue"))

	msgs, err := ch.Consume(q.Name,"", true, false, false,false,nil)

	for msg := range msgs {
		log.Printf("%s", msg.Body)
	}
}

